package com.dzm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzm.anno.RequiresPermissions;
import com.dzm.entity.Student;
import com.dzm.enums.Logical;
import com.dzm.service.StudentService;
import com.dzm.utils.R;
import com.dzm.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：Dzm
 * 时间：2024-08-21-11:03
 * 功能：
 */

@RestController
@RequestMapping("/api/student")
@RefreshScope   //加上不用重启，配置就会生效
public class StudentController {

    @Value("${student.name}")
    private String name;

    @Value("${server.port}")
    private String port;

    @Autowired
    private StudentService studentService;

    @GetMapping("/getStudentName")
    public R get() {
        return R.ok().data("name", name);
    }

    @RequiresPermissions(value = {"student:list", "student:abc"}, logical = Logical.OR)
    @GetMapping("list")
    public R list() {
        return R.ok().data("list", studentService.list());
    }

    @GetMapping("/getStudentById/{id}")
    public R getById(@PathVariable("id")Integer id) {
        return R.ok().data("stu", studentService.getById(id)).code(Integer.parseInt(port));
    }

    @RequiresPermissions(value = {"student:search"})
    @PostMapping("/search/{pageNum}/{pageSize}")
    public R search(@PathVariable("pageNum") Integer pageNum,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody SearchVo searchVo) {
        Page<Student> pageModel = studentService.searchStudent(pageNum, pageSize, searchVo);

        return R.ok().data("pageModel", pageModel);
    }

    @RequiresPermissions("student:getById")
    @GetMapping("/getById/{sid}")
    public R getStudentById(@PathVariable("sid") Integer sid) {
        Student student = studentService.getById(sid);

        return R.ok().data("stu", student);
    }

    @RequiresPermissions("student:add")
    @PostMapping("/add")
    public R addStudent(@RequestBody Student student) {
        studentService.save(student);

        return R.ok();
    }

    @RequiresPermissions("student:update")
    @PutMapping("/update")
    public R updateStudent(@RequestBody Student student) {
        studentService.updateById(student);

        return R.ok();
    }

    @RequiresPermissions("student:remove")
    @DeleteMapping("/remove")
    public R remove(@RequestBody List<Integer> ids) {
        studentService.removeBatchByIds(ids);

        return R.ok();
    }

}
